package com.ewei.support.javax.mail.util;

import javax.mail.Part;

import org.budo.support.lang.util.ReflectUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author limingwei
 */
public class MimeBodyPartUtil {
    private static final Logger log = Slf4j.getLogger();

    /**
     * @see javax.mail.internet.MimeBodyPart#getContent()
     * @see com.sun.mail.imap.IMAPBodyPart
     */
    public static void resetPart(Part part) {
        if (null == part) {
            log.error("resetPart of null, part=" + part);
            return;
        }

        try {
            if (ReflectUtil.hasField(part.getClass(), "cachedContent")) {
                ReflectUtil.setFieldValue(part, "cachedContent", null);
            } else {
                log.error("part do not has field cachedContent part.type=" + part.getClass().getName());
            }
        } catch (Throwable e) {
            log.error("resetPart error, " + e);
        }

        try {
            if (ReflectUtil.hasField(part.getClass(), "dh")) {
                ReflectUtil.setFieldValue(part, "dh", null);
            } else {
                log.error("part do not has field dh part.type=" + part.getClass().getName());
            }
        } catch (Throwable e) {
            log.error("resetPart error, " + e);
        }
    }
}